//
//  TabBarController.m
//  CarLifes
//
//  Created by yu on 2017/6/14.
//  Copyright © 2017年 jpas. All rights reserved.
//

#import "TabBarController.h"
#import "NavigationController.h"
#import "ServiceViewController.h"
#import "PlatformViewController.h"
#import "PhotoViewController.h"
#import "UIImage+CLImage.h"
#import "PlatformTabBarController.h"
#import "UIView+TransitionAnimation.h"
#import "WorkorderLoginViewController.h"


@interface TabBarController ()<UITabBarDelegate>

@end

@implementation TabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupAllChildViewControllers];
    self.tabBar.translucent = NO;
    
//    [[UITabBar appearance] setShadowImage:[UIImage new]];
//    [[UITabBar appearance] setBackgroundImage:[[UIImage alloc]init]];
//    [self dropShadowWithOffset:CGSizeMake(0, -1)
//                        radius:3
//                         color:[UIColor grayColor]
//                       opacity:0.3];
}

// 设置不允许屏幕旋转
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

- (void)dropShadowWithOffset:(CGSize)offset
                      radius:(CGFloat)radius
                       color:(UIColor *)color
                     opacity:(CGFloat)opacity {
    
    // Creating shadow path for better performance
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathAddRect(path, NULL, self.tabBar.bounds);
    self.tabBar.layer.shadowPath = path;
    CGPathCloseSubpath(path);
    CGPathRelease(path);
    
    self.tabBar.layer.shadowColor = color.CGColor;
    self.tabBar.layer.shadowOffset = offset;
    self.tabBar.layer.shadowRadius = radius;
    self.tabBar.layer.shadowOpacity = opacity;
    
    // Default clipsToBounds is YES, will clip off the shadow, so we disable it.
    self.tabBar.clipsToBounds = NO;
}

- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection
{
    [super traitCollectionDidChange:previousTraitCollection];
    if (@available(iOS 13.0, *)) {
            
//            NavigationController *navigationController = self.childViewControllers[3];
            
//            UIImage *newImage = nil;
//
//            if (previousTraitCollection.userInterfaceStyle == UIUserInterfaceStyleDark) {
//                newImage = [UIImage imageNamed:@"workorder_nor"];
//            }else{
//                newImage = [UIImage imageNamed:@"workorder_sel"];
//            }
//            navigationController.tabBarItem.image = [newImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        }
}

- (void)setupAllChildViewControllers {

    ServiceViewController *service = [[ServiceViewController alloc] init];
    [self setupChildViewController:service image:[UIImage imageNamed:@"service_normal"] selectImage:[UIImage imageWithOriginalName:@"service_select"] title:@"服务"];
    
    PlatformViewController *platform = [[PlatformViewController alloc] init];
    [self setupChildViewController:platform image:[UIImage imageNamed:@"platform_normal"] selectImage:[UIImage imageWithOriginalName:@"platform_select"] title:@"平台"];
    
    PhotoViewController *photo = [[PhotoViewController alloc] init];
    [self setupChildViewController:photo image:[UIImage imageNamed:@"photo_normal"] selectImage:[UIImage imageWithOriginalName:@"photo_select"] title:@"拍照"];
//    WorkorderLoginViewController *wo = [[WorkorderLoginViewController alloc] init];
//    [self setupChildViewController:wo image:[UIImage imageNamed:@"workorder_nor"] selectImage:[UIImage imageWithOriginalName:@"workorder_sel"] title:@"工单"];
}

/**
 添加子控制器

 @param viewController 子控制器
 @param image 图片
 @param selectImage 选中图片
 @param title item的标题
 */
- (void)setupChildViewController:(UIViewController *)viewController image:(UIImage *)image selectImage:(UIImage *)selectImage title:(NSString *)title{
    
    viewController.title = title;
    viewController.tabBarItem.image = image;
    viewController.tabBarItem.selectedImage = selectImage;
    
    NavigationController *nav = [[NavigationController alloc] initWithRootViewController:viewController];
    [self addChildViewController:nav];
}

/**
 *  设置TabbarItem的主题
 */
+ (void)initialize{
    
    NSMutableDictionary *attr = [NSMutableDictionary dictionary];
    attr[NSFontAttributeName] = [UIFont systemFontOfSize:10];
    attr[NSForegroundColorAttributeName] = [UIColor grayColor];
    
    NSMutableDictionary *selectedAttr = [NSMutableDictionary dictionary];
    selectedAttr[NSFontAttributeName] = [UIFont systemFontOfSize:10];
    selectedAttr[NSForegroundColorAttributeName] = kBaseColor;
    
    UITabBarItem *items = [UITabBarItem appearance];
    //    [items setTitlePositionAdjustment:UIOffsetMake(0, -1.)];
    [items setTitleTextAttributes:attr forState:UIControlStateNormal];
    [items setTitleTextAttributes:selectedAttr forState:UIControlStateSelected];
}

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
//    if ([item.title isEqualToString:@"平台"]) {
//        CLPlatformTabBarController *platform = [[CLPlatformTabBarController alloc] init];
//        
//        [KEY_WINDOW addTransitionAnimationWithDuration:0.5f type:TransitionPageCurl subType:FROM_BOTTOM];
//        KEY_WINDOW.rootViewController = platform;
//    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
